package com.bbtShop.core.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;

import java.io.*;
import java.util.Map;

/**
 * @Author manke
 * @Date 2020/10/11 13:15
 * @Description 循环ftl生成
 * @Version 0.0.1
 */
public class WordUtil {

    public static String testPaperWord (Map<String,Object> dataMap,String path,String templateName,String outFileName) throws IOException, TemplateException {
        Configuration configuration = new Configuration(new Version("2.3.0"));
        configuration.setDefaultEncoding("utf-8");

        configuration.setDirectoryForTemplateLoading(new File(path)); //模板地址
        //输出文件名称
        String fileName =  outFileName;
        File file = new File(path);
        if (!file.exists()){
            file.mkdir();
        }
        File outFile = new File(path+fileName);
        Template template = configuration.getTemplate(templateName, "utf-8");
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
        template.process(dataMap, out);
        out.close();
        return fileName;
    }
}
